<?php
class Db_TopPage extends MVC_SQL {
	public function __construct(){
		$this->setTable('ams_product_category');
	}
	
	public function getAllProductToday($oModel)
	{
		/*
		$today=date('Y-m-d');
		$sSql="SELECT * FROM ams_product pro LEFT JOIN ams_pictures pic ON pro.product_id = pic.pictures_product_id
		WHERE pro.product_date_added = '{$today}'";
		*/
		$sSql="SELECT * FROM ams_product pro LEFT JOIN ams_pictures pic ON pro.product_id = pic.pictures_product_id 
			ORDER BY SUBSTRING( pro.product_id, 1, 1 ) , CAST( SUBSTRING( pro.product_id, 2 ) AS UNSIGNED ) ASC
		";
		return $this->getRecordsetExtend($sSql);
	}
	
	public function getListGroupCategory($oModel)
	{
		$sSql="SELECT * FROM ams_group_category_statictis WHERE group_status =1 ";
		return $this->getRecordsetExtend($sSql);
	}
	
	public function getListChildGroupCategory($parent_id)
	{
		$sSql=" SELECT rt. * , r . *
				FROM ams_product_category rt
				INNER JOIN ams_group_category_statictis r ON ( 0
				OR r.group_child
				REGEXP CONCAT( '', '^', rt.product_category_id, '$' )
				OR r.group_child
				REGEXP CONCAT( '', '^', rt.product_category_id, ' ' )
				OR r.group_child
				REGEXP CONCAT( '', ' ', rt.product_category_id, ' ' )
				OR r.group_child
				REGEXP CONCAT( '', ' ', rt.product_category_id, '$' ) )
				WHERE r.group_id ={$parent_id}";
		return $this->getRecordsetExtend($sSql);
	}

	public function getQtyProductOfCategory($cat_id)
	{
		$today=date('Y-m-d');
		$sSql="SELECT * FROM ams_product
		WHERE product_date_added = '{$today}' AND product_category_id = {$cat_id}";
		return $this->getNumRow($sSql);
	}
	
//public function getAllStaticCategory($oModel)
//	{
//		$sSql="SELECT * FROM ams_product_category WHERE product_category_statistics =1 ";
//		
//		return $this->getRecordsetExtend($sSql);
//	}
//	
	public function getListStaticCategoryChild($parent_id)
	{
		$sSql="SELECT * FROM ams_product_category WHERE 1 AND product_category_status =1 AND product_category_parent_id = {$parent_id}";
		return $this->getRecordsetExtend($sSql);
	}
	
	/**
	 * MESSAGE BOARD
	 */
	 public function getListMessage($oModel)
	 {
	 	$today=date('Y-m-d');
		$sSql="SELECT * FROM ams_message_board amb
		 	   INNER JOIN ams_admin aa ON
			   amb.message_from = aa.admin_id
			   WHERE date_format(amb.message_date,'%Y-%m-%d') = '{$today}'
			   ORDER BY message_date";
		return $this->getRecordsetExtend($sSql);
		 
	 }
	 
	public function getInfoUserById($id)
	{
		$sSql="SELECT admin_username as user_to FROM ams_admin WHERE admin_id ={$id} ";
		return $this->getRecordsetByPKExtend($sSql);
	}
	
	public function check_size($id)
	{
		$sSql="SELECT * FROM `ams_product_attribute` WHERE product_id = {$id} ";
		return $this->getRecordsetExtend($sSql);
	}
	public function getTotalItems(){
		$sSQL="SELECT product_id FROM ams_product";
		return $this->getNumRow($sSQL);
	}
	public function getTitleItems(){
		$sSQL="SELECT product_id FROM ams_product WHERE product_title<>''";
		return $this->getNumRow($sSQL);
	}
	public function getPhotoItems(){
		$sSQL="SELECT product_id FROM ams_product WHERE product_picture1 <>''  OR product_picture2 <>'' OR product_picture3 <>''";
		return $this->getNumRow($sSQL);
	}
	public function getSizeItems(){
		$sSQL="SELECT product_id FROM ams_product WHERE product_size <> ''";
		return $this->getNumRow($sSQL);
	}
	public function getExplainationItems(){
		$sSQL="SELECT product_id FROM ams_product WHERE product_explanation <> ''";
		return $this->getNumRow($sSQL);
	}
	public function getDoneItems(){
		$sSQL="SELECT product_id FROM ams_product 
		WHERE (product_explanation <> '') AND (product_size <> '') AND (product_title<>'') AND 
		(product_picture1 <>''  OR product_picture2 <>'' OR product_picture3 <>'')";
		return $this->getNumRow($sSQL);
	}
}